package cn.zh.controller;

import cn.hutool.core.util.IdUtil;
import cn.zh.commons.ResponseResult;
import cn.zh.entity.Houseresource;
import cn.zh.service.ResourceService;
import cn.zh.vo.UserVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.handler.RequestMatchResult;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * @author : 张航
 * @date : 2024-05-30 15:15
 * @description:
 **/
@RestController
@RequestMapping("resource")
public class ResourceController {
    @Resource
    private ResourceService resourceService;


    @RequestMapping("page")
    public ResponseResult page(Integer page,Integer limit,String pattern){
        return resourceService.page(page,limit,pattern);
    }

    @RequestMapping("getByUserName")
    public ResponseResult getByUserName(String houseUsername){
        return resourceService.houseUsername(houseUsername);
    }
    @RequestMapping("audit")
    public ResponseResult audit(UserVo userVo){
        return resourceService.audit(userVo);
    }
    @RequestMapping("delete")
    public ResponseResult delete(Integer id){

        return resourceService.delete(id);
    }

    //前端现实的展示房源
    @RequestMapping("front/list")
    public  ResponseResult list(Integer page,Integer limit,String pattern){
        return resourceService.list(page,limit,pattern);
    }
    //知道一共有多少房源
    @RequestMapping("front/count")
    public ResponseResult count (){
        return resourceService.count();
    }
    @RequestMapping("front/listup")
    public ResponseResult listup (Integer page,Integer limit,String pattern){
        return resourceService.listup(page,limit,pattern);
    }
    @RequestMapping("front/listdown")
    public ResponseResult listdown (Integer page,Integer limit,String pattern){
        return resourceService.listdown(page,limit,pattern);
    }
    @RequestMapping("front/byId")
    public ResponseResult byId (Integer id){
        return resourceService.byId(id);
    }

    //房源图片
    @RequestMapping("upload")
    @ResponseBody
    public ResponseResult uploadSlide(MultipartFile file) throws IOException {
        //判定文件是否是空文件
        if (file.isEmpty()){
            return ResponseResult.fail().message("请选择文件");
        }
        //文件上传的步骤
        String originalFilename = file.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);  //png jpg
        String s = IdUtil.simpleUUID();
        //文件在服务器的唯一标识
        String lastName = s+"."+filenameExtension;
        //TODO
        File file1 = new File("D:/IDEAFrame/upload/resource/");
        if (!file1.exists()){
            file1.mkdir();
        }

        File target = new File(file1, lastName);

        file.transferTo(target);

        return ResponseResult.success().data(lastName);
    }

    //房源信息上传
    @RequestMapping("publish")
    @ResponseBody
    public ResponseResult publish(Houseresource houseresource){
        return resourceService.publish(houseresource);
    }

    //用户查询自己发布的房源信息并分页
    @RequestMapping("UserPage")
    public ResponseResult UserPage(Integer page,Integer limit,String pattern,String username){
        return resourceService.UserPage(page,limit,pattern,username);
    }
}
